package com.lhkj.ct.admin.modules.scheduling.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.scheduling.model.dto.ScheduleDto;
import com.lhkj.ct.meta.modules.scheduling.model.dto.ScheduleSearch;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheduling;
import com.lhkj.ct.meta.modules.scheduling.model.vo.ScheduleMonthVo;
import com.lhkj.ct.meta.modules.scheduling.service.SchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 排班记录 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-27
 */
@RestController
@RequestMapping("api/plat/scheduling/manage")
@Api(tags = {"4.1排班管理"})
@Validated
public class SchedulingManageController {

    @Resource
    private SchedulingService schedulingService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "日历列表")
    @GetMapping("calendar")
    public ActionResult<List<ScheduleMonthVo>> selectAllByMonth(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectAllByMonth(search));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "日历详情")
    @GetMapping("allByDate")
    public ActionResult<List<TblScheduling>> selectAllByDate(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectAllByDate(search));
    }


    @ApiOperationSupport(order = 3)
    @PreventDuplicateSubmit
    @PostMapping(value = "insert")
    @ApiOperation(value = "新增")
    @Log(title = "排班管理", actionType = ActionType.INSERT)
    public ActionResult<Void> insert(@Validated @RequestBody ScheduleDto scheduleDto) {
        return ActionResult.judge(schedulingService.save(scheduleDto));
    }

    @ApiOperationSupport(order = 4)
    @PutMapping(value = "update")
    @ApiOperation(value = "修改")
    @Log(title = "排班管理", actionType = ActionType.UPDATE)
    public ActionResult<Void> update(@Validated @RequestBody TblScheduling tblScheduling) {
        return ActionResult.judge(schedulingService.updateById(tblScheduling));
    }

    @ApiOperationSupport(order = 5)
    @DeleteMapping(value = "cancelDay")
    @ApiOperation(value = "按天取消")
    @Log(title = "排班管理", actionType = ActionType.DELETE)
    public ActionResult<Void> cancelByDate(@Validated(ValGroups.Delete.class) @RequestBody TblScheduling date) {
        return ActionResult.judge(schedulingService.cancelByDate(date.getScheDate()));
    }

    @DeleteMapping("cancel")
    @ApiOperation(value = "取消排班接口", notes = "支持批量")
    public ActionResult<Void> cancel(@NotEmpty(message = "请选择需要取消的排班") @RequestBody List<String> ids) {
        int count = schedulingService.cancel(ids);
        return ActionResult.success(String.format("取消成功，共取消【%d】条排班", count));
    }

    @PutMapping("stop")
    @ApiOperation(value = "停号", notes = "支持批量")
    public ActionResult<Void> stop(@NotEmpty(message = "请选择需要暂停的排班") @RequestBody List<String> ids) {
        int count = schedulingService.stop(ids);
        return ActionResult.success(String.format("操作成功，共暂停【%d】个排班", count));
    }

    @PutMapping("restore")
    @ApiOperation(value = "恢复", notes = "支持批量")
    public ActionResult<Void> restore(@NotEmpty(message = "请选择需要恢复的排班") @RequestBody List<String> ids) {
        int count = schedulingService.restore(ids);
        return ActionResult.success(String.format("操作成功，共恢复【%d】个排班", count));
    }
}
